/*
 * Ventas.java
 *
 * Created on 16 de octubre de 2006, 09:10 AM
 */

package inc.jmc.cyberboard.ui.ventas;
import inc.jmc.cyberboard.common.DefaultLookAndFeel;
import inc.jmc.cyberboard.common.ImageLoader;
import inc.jmc.cyberboard.common.StringExceptions;
import inc.jmc.cyberboard.common.NumFormat;
import inc.jmc.cyberboard.dao.AccountUserDao;
import inc.jmc.cyberboard.dao.Dao;
import inc.jmc.cyberboard.dao.HistoricoVentasDao;
import inc.jmc.cyberboard.entitybeans.HistoricoVentas;
import inc.jmc.cyberboard.log.Log;
import inc.jmc.cyberboard.ui.CyberManager;
import inc.jmc.cyberboard.ui.print.ticket.TicketGenerator;
import inc.jmc.cyberboard.ztrashcan.CatalogoListProductos;
import java.awt.Color;
import java.awt.Insets;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import org.apache.log4j.Logger;
/**
 *
 * @author  Juan Miguel Olguin Salguero
 */
public class VentasFrm extends javax.swing.JInternalFrame {
    private String user="";
    static Logger logger = Logger.getLogger(VentasFrm.class.getName());
    private CatalogoGridProductos catalogoGridProd=null;
    private CatalogoIconProductos catalogoIconProd=null;
    private DetalleGridVentas detalleGrid=null;
    private HistoricoGridVentas historicoGrid=null;
    private CyberManager cyberM;
    
    public VentasFrm() {
        logger.info("VentasFrm()---->ejecutado");
        initComponents();
        this.setFrameIcon(ImageLoader.images[ImageLoader.ICON_FRAME_MAIN]);
        
        this.areaSelecProdsPanel.removeAll();
        //Aqui ponemos los verdaderos grids que se van a ver en tiempo de ejecucion
        //en el area de detalle de ventas y 
        //en el area de historico de ventas.
        detalleGrid=new DetalleGridVentas(this);
        historicoGrid=new HistoricoGridVentas(null);
        this.areaDetalleVentasPanel.removeAll();
        this.areaDetalleVentasPanel.add(detalleGrid);
        this.areaHistoricoVentasPanel.removeAll();
        this.areaHistoricoVentasPanel.add(historicoGrid);
        
        //Construyendo los tables
        catalogoGridProd = new CatalogoGridProductos(this);
        catalogoIconProd = new CatalogoIconProductos(this);
        
        
        jSplitPane1.setDividerLocation(350);
        jSplitPane2.setDividerLocation(430);
        this.setTitle("Modulo de Registro de Ventas");
        
        //ponemos el fondo de la caja de texto , en blanco, 
        //ya no esta asi, por estar inhabilitados
        fechaTxt.setBackground(DefaultLookAndFeel.NotEnabledTextBackground); 
        usuarioTxt.setBackground(DefaultLookAndFeel.NotEnabledTextBackground);
        this.articulosTxt.setFont(DefaultLookAndFeel.TableFont4);
        guardaVentaButton.setMargin(new Insets(0,0,0,0));
        this.articulosTxt.setBackground(DefaultLookAndFeel.TableBackground);
        this.pagoTxt.setFont(DefaultLookAndFeel.TableFont4);
        this.cambioTxt.setFont(DefaultLookAndFeel.TableFont4);
        this.cambioTxt.setBackground(DefaultLookAndFeel.TableBackground);
        this.ventaTotalTxt.setFont(DefaultLookAndFeel.TableFont4);
        this.ventaTotalTxt.setBackground(DefaultLookAndFeel.TableBackground);
        this.ivaTxt.setFont(DefaultLookAndFeel.TableFont4);
        this.ivaTxt.setBackground(DefaultLookAndFeel.TableBackground);
        this.subtotalTxt.setFont(DefaultLookAndFeel.TableFont4);
        this.subtotalTxt.setBackground(DefaultLookAndFeel.TableBackground);
        this.viewModeCmb.removeAllItems();
        this.viewModeCmb.addItem(ImageLoader.images[ImageLoader.VISTA_TABLA]);
        this.viewModeCmb.addItem(ImageLoader.images[ImageLoader.VISTA_ICONOS]);
        
        fechaTxt.setText(getFecha());
        usuarioTxt.setText(user);
        this.areaSelecProdsPanel.requestFocus();
        settingInternalFrame();
    }
    
    void settingInternalFrame()
    {
        this.setIconifiable(false);
        //this.setSize(947,517);
        this.setSize(877,517);
        this.setResizable(true);
        this.setMaximizable(false);
        this.setClosable(true);
        this.updateUI();
    }
    
    
    String getFecha()
    {
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
        String aux=sdf.format(new Date());
        return aux;
    }

    public JComboBox getAscendenteCmb() {
        return ascendenteCmb;
    }

    public JComboBox getCampoOrdenarCmb() {
        return campoOrdenarCmb;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
        usuarioTxt.setText(user);
    }


    /** Creates new form Ventas */
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jSplitPane2 = new javax.swing.JSplitPane();
        jPanel6 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        fechaTxt = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        usuarioTxt = new javax.swing.JTextField();
        jPanel8 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jPanel14 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        ventaTotalTxt = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        articulosTxt = new javax.swing.JTextField();
        guardaVentaButton = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        ivaTxt = new javax.swing.JTextField();
        subtotalTxt = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        pagoTxt = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        cambioTxt = new javax.swing.JTextField();
        areaDetalleVentasPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel9 = new javax.swing.JPanel();
        jPanel12 = new javax.swing.JPanel();
        jPanel13 = new javax.swing.JPanel();
        areaHistoricoVentasPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        campoOrdenarCmb = new javax.swing.JComboBox();
        ascendenteCmb = new javax.swing.JComboBox();
        viewModeCmb = new javax.swing.JComboBox();
        refreshDataButton = new javax.swing.JButton();
        jPanel11 = new javax.swing.JPanel();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        areaSelecProdsPanel = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Modulo de Registro de Ventas");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        getContentPane().setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.BorderLayout());

        jSplitPane1.setDividerLocation(200);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel7.setMaximumSize(new java.awt.Dimension(100, 100));
        jPanel7.setMinimumSize(new java.awt.Dimension(100, 100));
        jPanel7.setPreferredSize(new java.awt.Dimension(100, 100));
        jPanel7.setLayout(new java.awt.BorderLayout());

        jSplitPane2.setDividerLocation(450);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jPanel6.setMaximumSize(new java.awt.Dimension(100, 200));
        jPanel6.setMinimumSize(new java.awt.Dimension(100, 200));
        jPanel6.setPreferredSize(new java.awt.Dimension(100, 200));
        jPanel6.setLayout(new java.awt.BorderLayout());

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Descripcion de Venta"));
        jPanel5.setMaximumSize(new java.awt.Dimension(100, 150));
        jPanel5.setMinimumSize(new java.awt.Dimension(100, 150));
        jPanel5.setOpaque(false);
        jPanel5.setPreferredSize(new java.awt.Dimension(100, 44));
        jPanel5.setLayout(null);

        jLabel5.setText("Fecha:");
        jPanel5.add(jLabel5);
        jLabel5.setBounds(10, 15, 50, 20);

        fechaTxt.setEditable(false);
        jPanel5.add(fechaTxt);
        fechaTxt.setBounds(70, 15, 80, 20);

        jLabel7.setText("Atendido por:");
        jPanel5.add(jLabel7);
        jLabel7.setBounds(160, 15, 66, 20);

        usuarioTxt.setEditable(false);
        jPanel5.add(usuarioTxt);
        usuarioTxt.setBounds(235, 15, 110, 20);

        jPanel6.add(jPanel5, java.awt.BorderLayout.NORTH);

        jPanel8.setPreferredSize(new java.awt.Dimension(10, 125));
        jPanel8.setLayout(new java.awt.BorderLayout());
        jPanel8.add(jPanel10, java.awt.BorderLayout.WEST);

        jPanel14.setMaximumSize(new java.awt.Dimension(60, 32767));
        jPanel14.setMinimumSize(new java.awt.Dimension(60, 310));
        jPanel14.setPreferredSize(new java.awt.Dimension(510, 100));
        jPanel14.setLayout(null);

        jLabel2.setText("Venta Total");
        jPanel14.add(jLabel2);
        jLabel2.setBounds(210, 70, 70, 14);

        ventaTotalTxt.setEditable(false);
        ventaTotalTxt.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPanel14.add(ventaTotalTxt);
        ventaTotalTxt.setBounds(270, 70, 230, 30);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Articulos");
        jPanel14.add(jLabel4);
        jLabel4.setBounds(10, 10, 50, 14);

        articulosTxt.setEditable(false);
        articulosTxt.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPanel14.add(articulosTxt);
        articulosTxt.setBounds(60, 10, 130, 30);

        guardaVentaButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        guardaVentaButton.setMnemonic('R');
        guardaVentaButton.setText("Registrar Venta");
        guardaVentaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardaVentaButtonActionPerformed(evt);
            }
        });
        jPanel14.add(guardaVentaButton);
        guardaVentaButton.setBounds(50, 100, 140, 20);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Iva");
        jPanel14.add(jLabel6);
        jLabel6.setBounds(260, 40, 40, 14);

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel8.setText("Subtotal");
        jPanel14.add(jLabel8);
        jLabel8.setBounds(250, 10, 50, 14);

        ivaTxt.setEditable(false);
        ivaTxt.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPanel14.add(ivaTxt);
        ivaTxt.setBounds(300, 40, 200, 30);

        subtotalTxt.setEditable(false);
        subtotalTxt.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jPanel14.add(subtotalTxt);
        subtotalTxt.setBounds(300, 10, 200, 30);

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("pago");
        jPanel14.add(jLabel9);
        jLabel9.setBounds(10, 50, 50, 14);

        pagoTxt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                pagoTxtKeyReleased(evt);
            }
        });
        jPanel14.add(pagoTxt);
        pagoTxt.setBounds(60, 40, 130, 30);

        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setText("cambio");
        jPanel14.add(jLabel10);
        jLabel10.setBounds(0, 80, 60, 14);

        cambioTxt.setEditable(false);
        jPanel14.add(cambioTxt);
        cambioTxt.setBounds(60, 70, 130, 30);

        jPanel8.add(jPanel14, java.awt.BorderLayout.EAST);

        jPanel6.add(jPanel8, java.awt.BorderLayout.SOUTH);

        areaDetalleVentasPanel.setLayout(new java.awt.BorderLayout());

        jTable1.setFont(new java.awt.Font("Lucida Console", 1, 11)); // NOI18N
        jTable1.setForeground(new java.awt.Color(7, 56, 252));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        areaDetalleVentasPanel.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel6.add(areaDetalleVentasPanel, java.awt.BorderLayout.CENTER);

        jSplitPane2.setTopComponent(jPanel6);

        jPanel9.setLayout(new java.awt.BorderLayout());
        jPanel9.add(jPanel12, java.awt.BorderLayout.NORTH);
        jPanel9.add(jPanel13, java.awt.BorderLayout.SOUTH);

        areaHistoricoVentasPanel.setLayout(new java.awt.BorderLayout());

        jTable2.setFont(new java.awt.Font("Lucida Console", 1, 11)); // NOI18N
        jTable2.setForeground(new java.awt.Color(7, 56, 252));
        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        areaHistoricoVentasPanel.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel9.add(areaHistoricoVentasPanel, java.awt.BorderLayout.CENTER);

        jSplitPane2.setRightComponent(jPanel9);

        jPanel7.add(jSplitPane2, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel7, java.awt.BorderLayout.CENTER);

        jSplitPane1.setRightComponent(jPanel2);

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel4.setMaximumSize(new java.awt.Dimension(100, 20));
        jPanel4.setMinimumSize(new java.awt.Dimension(100, 20));
        jPanel4.setPreferredSize(new java.awt.Dimension(100, 50));
        jPanel4.setLayout(null);

        jLabel1.setText("<html>Tipo de vista</html>");
        jPanel4.add(jLabel1);
        jLabel1.setBounds(4, 4, 80, 15);

        jLabel3.setLabelFor(usuarioTxt);
        jLabel3.setText("<html>Ordenar</html>");
        jPanel4.add(jLabel3);
        jLabel3.setBounds(160, 4, 50, 40);

        campoOrdenarCmb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nombre", "Precio", "Descripcion" }));
        campoOrdenarCmb.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                campoOrdenarCmbItemStateChanged(evt);
            }
        });
        jPanel4.add(campoOrdenarCmb);
        campoOrdenarCmb.setBounds(220, 0, 120, 20);

        ascendenteCmb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ascendente", "Descendente" }));
        ascendenteCmb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ascendenteCmbActionPerformed(evt);
            }
        });
        jPanel4.add(ascendenteCmb);
        ascendenteCmb.setBounds(220, 24, 120, 20);

        viewModeCmb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        viewModeCmb.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                viewModeCmbItemStateChanged(evt);
            }
        });
        jPanel4.add(viewModeCmb);
        viewModeCmb.setBounds(4, 20, 50, 30);

        refreshDataButton.setText("Actualiza");
        refreshDataButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshDataButtonActionPerformed(evt);
            }
        });
        jPanel4.add(refreshDataButton);
        refreshDataButton.setBounds(65, 20, 80, 23);

        jPanel3.add(jPanel4, java.awt.BorderLayout.NORTH);

        jPanel11.setMaximumSize(new java.awt.Dimension(60, 100));
        jPanel11.setMinimumSize(new java.awt.Dimension(60, 100));
        jPanel11.setPreferredSize(new java.awt.Dimension(60, 100));
        jPanel11.setLayout(null);

        addButton.setText(">>");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });
        jPanel11.add(addButton);
        addButton.setBounds(0, 80, 50, 23);

        removeButton.setText("<<");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });
        jPanel11.add(removeButton);
        removeButton.setBounds(0, 110, 50, 23);

        jPanel3.add(jPanel11, java.awt.BorderLayout.EAST);

        areaSelecProdsPanel.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                areaSelecProdsPanelComponentAdded(evt);
            }
        });
        areaSelecProdsPanel.setLayout(new java.awt.BorderLayout());

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(jTable3);

        areaSelecProdsPanel.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jPanel3.add(areaSelecProdsPanel, java.awt.BorderLayout.CENTER);

        jSplitPane1.setLeftComponent(jPanel3);

        jPanel1.add(jSplitPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void pagoTxtKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pagoTxtKeyReleased

        try{
        	String arg=pagoTxt.getText();
        	float content1 = Float.parseFloat(arg);
            String venta1 = ventaTotalTxt.getText();
            float ventaf=NumFormat.parseMoney(venta1);
            float resta=content1-ventaf;
            cambioTxt.setText(NumFormat.nf_00.format(resta));
        }
        catch(Exception ex)
        {
        	cambioTxt.setText("");
        }
        
        
    }//GEN-LAST:event_pagoTxtKeyReleased

    private void guardaVentaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardaVentaButtonActionPerformed

        try{
            if(detalleGrid.dtm.getCountArticulos()==0)
                return;

        
            HistoricoVentasDao ventasDao = new HistoricoVentasDao();
            java.sql.Connection conn=Dao.getConnection();
            int idUser = 0;
            AccountUserDao accUserDao = new AccountUserDao();
            idUser = accUserDao.getIdAccountUser(conn,this.user);
            //idUser = accUserDao.getIdAccountUser(conn,"jolguin");
            //Primero se solicita que se creen datos a partir 
            //de lo que se capturo en la tabla de detalle de la compra
            //mas los datos de iva, subtotal, y total
            //con todos estos datos se crea un 
            //historico de ventas, que incluye el detalle de la venta 
            HistoricoVentas venta=this.detalleGrid.getHistoricoVentasBean(idUser);

            ///despues se solicita que esa nueva venta , se agregue a 
            ///la base de datos
            int idVenta = ventasDao.addHistoricoVentas(conn,venta);
            //ahora mandamos a imprimir nuestro ticket
            TicketGenerator generator = new TicketGenerator(this.cyberM.getTicketTemplateFrm().getTicketTemplateVO());
            String generateTicket = generator.generateTicket(venta,this.cyberM.getAuthorizedUser(),
            		NumFormat.money(venta.getSubtotalNoIva()),
            		NumFormat.money(venta.getMontoIva()),
            		NumFormat.money(venta.getMontoTotal())
            		);
            logger.info("este es el ticket que se va a imprimir");
            logger.info(generateTicket);
            
            ///
            historicoGrid.loadProductsBD();
            //y ahora borramos lo que habia en el detalle , para dejar el espacio libre para una nueva venta
            this.detalleGrid.selectAllItems();
            this.detalleGrid.removeSelectedItems();
            //cerramos la conexion
            conn.close();
            logger.info("Ya guardamos en la bd la nueva venta");
            pagoTxt.setText("");
            cambioTxt.setText("");
            JOptionPane.showMessageDialog(this,"Venta Guardada",
                    "BD: Datos guardados",
                    JOptionPane.INFORMATION_MESSAGE);
            logger.info(venta);
        } catch(java.sql.SQLException ex){
            ex.printStackTrace();
            ///Desplegamos el mensaje de error en una ventana
            JOptionPane.showMessageDialog(this,new StringExceptions(ex).getMsg(),
                    "Error en operacion de BD",
                    JOptionPane.ERROR_MESSAGE);
        } catch(ClassNotFoundException ex){
            ex.printStackTrace();
            ///Desplegamos el mensaje de error en una ventana
            JOptionPane.showMessageDialog(this,ex.getMessage(),
                    "Error en operacion de BD",
                    JOptionPane.ERROR_MESSAGE);
        }
         catch(Exception ex){
            ex.printStackTrace();
            ///Desplegamos el mensaje de error en una ventana
            JOptionPane.showMessageDialog(this,ex.getMessage(),
                    "Error en operacion de BD",
                    JOptionPane.ERROR_MESSAGE);
        }
        
    }//GEN-LAST:event_guardaVentaButtonActionPerformed

    public JTextField getVentaTotalTxt() {
        return ventaTotalTxt;
    }

    public JTextField getArticulosTxt() {
        return articulosTxt;
    }


    private void refreshDataButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshDataButtonActionPerformed

         try{
            this.catalogoGridProd.loadProductsBD();
            this.catalogoIconProd.loadProductsBD();
            this.historicoGrid.loadProductsBD();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        
        
        
    }//GEN-LAST:event_refreshDataButtonActionPerformed

    public void refreshDataButtonActionPerformed() {
    	refreshDataButtonActionPerformed(null);
    }
    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed

        this.detalleGrid.removeSelectedItems();
    }//GEN-LAST:event_removeButtonActionPerformed

    private void areaSelecProdsPanelComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_areaSelecProdsPanelComponentAdded

        logger.info("Componente agregado en areaSelecProdsPanel, hay "+areaSelecProdsPanel.getComponents().length+" componentes");
        for(int i=0;i<areaSelecProdsPanel.getComponents().length;++i)
        {
            logger.info(""+i+".-Lista de componentes de areaSelecProdsPanel--->"+areaSelecProdsPanel.getComponents()[i].getClass().getName());
        
        }
        
        
    }//GEN-LAST:event_areaSelecProdsPanelComponentAdded

    private void viewModeCmbItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_viewModeCmbItemStateChanged

        int ndx1=viewModeCmb.getSelectedIndex();
        if(ndx1==0)
        {
            this.areaSelecProdsPanel.removeAll();
            this.areaSelecProdsPanel.add(catalogoGridProd, java.awt.BorderLayout.CENTER);
        }
        else
        {
            this.areaSelecProdsPanel.removeAll();
            this.areaSelecProdsPanel.add(catalogoIconProd, java.awt.BorderLayout.CENTER);
        }
        this.updateUI();
        
    }//GEN-LAST:event_viewModeCmbItemStateChanged

    private void ascendenteCmbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ascendenteCmbActionPerformed

        this.catalogoGridProd.ordenamientoClic();
    }//GEN-LAST:event_ascendenteCmbActionPerformed

    private void campoOrdenarCmbItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_campoOrdenarCmbItemStateChanged

        this.catalogoGridProd.ordenamientoClic();
       
    }//GEN-LAST:event_campoOrdenarCmbItemStateChanged

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed

        ArrayList arry=this.catalogoGridProd.getSelectedItems();
        this.detalleGrid.addItems(arry);
        
        this.catalogoGridProd.getProductosTable().requestFocus();
        
        
    }//GEN-LAST:event_addButtonActionPerformed

    public void addFromCatalogoGridProductos(ArrayList arry){
        this.detalleGrid.addItems(arry);
        this.catalogoGridProd.getProductosTable().requestFocus();
    }
    public DetalleGridVentas getDetalleGrid() {
        return detalleGrid;
    }
    


    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized

        //logger.info(this.getClass().getName()+":"+this.getSize().width+","+this.getSize().height+"    ");
        //logger.info(jSplitPane1.getClass().getName()+":localizacion del divisor  :"+jSplitPane1.getDividerLocation());
    }//GEN-LAST:event_formComponentResized

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentasFrm().setVisible(true);
            }
        });
    }

    public javax.swing.JTextField getIvaTxt() {
        return ivaTxt;
    }

    public javax.swing.JTextField getSubtotalTxt() {
        return subtotalTxt;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JPanel areaDetalleVentasPanel;
    private javax.swing.JPanel areaHistoricoVentasPanel;
    private javax.swing.JPanel areaSelecProdsPanel;
    private javax.swing.JTextField articulosTxt;
    private javax.swing.JComboBox ascendenteCmb;
    private javax.swing.JTextField cambioTxt;
    private javax.swing.JComboBox campoOrdenarCmb;
    private javax.swing.JTextField fechaTxt;
    private javax.swing.JButton guardaVentaButton;
    private javax.swing.JTextField ivaTxt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTextField pagoTxt;
    private javax.swing.JButton refreshDataButton;
    private javax.swing.JButton removeButton;
    private javax.swing.JTextField subtotalTxt;
    private javax.swing.JTextField usuarioTxt;
    private javax.swing.JTextField ventaTotalTxt;
    private javax.swing.JComboBox viewModeCmb;
    // End of variables declaration//GEN-END:variables

	/**
	 * @return the catalogoGridProd
	 */
	public CatalogoGridProductos getCatalogoGridProd() {
		return catalogoGridProd;
	}

	/**
	 * @return the pagoTxt
	 */
	public javax.swing.JTextField getPagoTxt() {
		return pagoTxt;
	}

	/**
	 * @return the cambioTxt
	 */
	public javax.swing.JTextField getCambioTxt() {
		return cambioTxt;
	}

	public CyberManager getCyberM() {
		return cyberM;
	}

	public void setCyberM(CyberManager cyberM) {
		this.cyberM = cyberM;
	}
    
}
